package nz.co.senanque.addressbook.session;

import java.util.List;

import nz.co.senanque.addressbook.instances.Person;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
/**
 * @author Roger Parkinson
 *
 */
public class AddressBookDAO {

	@Autowired private SessionFactory sessionFactory;

	/**
	* This method is used to create the user
	*/
	@Transactional
	public void createPerson(Person person)
	{
		Session session = sessionFactory.getCurrentSession();
		session.save(person);
	}

	/**
	* This method is used to get all the user names from the table User Detail
	* @return list of user names
	*/
	@Transactional(readOnly=true)
	public List<Person> getAllPersons()
	{
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery("from nz.co.senanque.addressbook.instances.Person").list();
	}

	/**
	* This method is used to delete an user
	* @param userDetail
	*/
	@Transactional
	public void deleteUser(Person Person)
	{
		Session session = sessionFactory.getCurrentSession();
		session.delete(Person);
	}

	protected SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	protected void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
}
